/*
* Copyright (C) 2021 ~ 2022 Uniontech Technology Co., Ltd.
*
* Author:      zhangbingyi <zhangbingyi@uniontech.com>
*
* Maintainer:  zhangbingyi <zhangbingyi@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "usbwhitelistdlg.h"
#include "common.h"
#include "widgets/defendertableheaderview.h"
#include "compixmap.h"

#include <QLabel>
#include <QFrame>
//#include <DTitlebar>
//#include <DTipLabel>
#include <QComboBox>
//#include <DIconButton>
#include <QHeaderView>
//#include <DFontSizeManager>
//#include <DBlurEffectWidget>
//#include <DPinyin>
#include <QFileDialog>
#include <QPushButton>
//#include <DWarningButton>

#define DIALOG_WIDTH 710 // dialog  宽度
#define DIALOG_MINIMUM_HEIGHT 573 // dialog  高度

#define LEFT_MARGINS 20 // 左边距
#define TOP_MARGINS 0 // 上边距
#define RIGHT_MARGINS 20 // 右边距离
#define BOTTOM_MARGINS 10 // 下边距离
#define SPACING 0 // 布局间隙

#define TABLEVIEW_WIDTH 472 // tableview 宽度
#define TABLEVIEW_HEIGHT 240 // tableview 高度
#define TABLEVIEW_ROW_HEIGHT 48 // tableview 每列高度

#define PB_CANCLE_WIDTH 170 // 取消按钮宽度
#define PB_CANCLE_HEIGHT 36 // 取消按钮高度

#define LINE_HEIGHT 28 // 分割线高度

// 白名单导入数据校验
#define WHITELIST_TEMPLATE_COL_COUNT 5
#define WHITELIST_TEMPLATE_ITEM_CHAR_MAX_LENTH 256

// 自动化标签
#define SET_ACC_NAME(control, name) SET_ACCESS_NAME(control, USBWhiteListDlg, name)

USBWhiteListDlg::USBWhiteListDlg(USBStorageSecurityModel *model, QWidget *parent)
    : QFrame(parent)
    , m_usbStorageSecurityModel(model)
//    , m_blurEffect(nullptr)
    , m_headerDelegate(nullptr)
    , m_whitelistTable(nullptr)
    , m_standardItemModel(nullptr)
    , m_downloadBtn(nullptr)
    , m_importBtn(nullptr)
    , m_USBWhitelistDelegate(nullptr)
    , m_USBDeviceDetails(nullptr)
{
    SET_ACC_NAME(this, USBWhiteListDialogMainWindow);

    // 設置ObejctName
    setObjectName("USBWhiteListDialogMainWindow");

    installEventFilter(this);

    // 大小设置
    this->resize(DIALOG_WIDTH, DIALOG_MINIMUM_HEIGHT);
    this->setFixedWidth(DIALOG_WIDTH);
    this->setFixedHeight(DIALOG_MINIMUM_HEIGHT);
    // 设置为Dialog
    setWindowFlags(Qt::Dialog);
    // 设置为阻态
    setWindowModality(Qt::ApplicationModal);
    // 背景透明化
    //setAttribute(Qt::WA_TranslucentBackground);
    // 界面初始化
    initUI();
    // 信号槽连接
    initConnection();
    // 下拉框显示
    createEditor();
}

USBWhiteListDlg::~USBWhiteListDlg()
{
}

// 布局
void USBWhiteListDlg::initUI()
{
    // 主布局
    QVBoxLayout *mainlayout = new QVBoxLayout(this);
    setLayout(mainlayout);
//    // 背景透明化
//    m_blurEffect = new DBlurEffectWidget(this);
//    m_blurEffect->setBlendMode(DBlurEffectWidget::BehindWindowBlend);
//    // 设置模糊大小
//    m_blurEffect->resize(size());
//    m_blurEffect->lower();

    // 边距离
    mainlayout->setContentsMargins(LEFT_MARGINS - 20, TOP_MARGINS, RIGHT_MARGINS - 20, BOTTOM_MARGINS);
    mainlayout->setSpacing(SPACING);
    mainlayout->setAlignment(Qt::AlignmentFlag::AlignTop);
    QHBoxLayout *titleBarLayout = new QHBoxLayout();
    titleBarLayout->setContentsMargins(0, 0, 0, 0);

//    // 右上角关闭按钮
//    DTitlebar *titleBar = new DTitlebar(this);
//    titleBar->setFixedWidth(this->width());
//    titleBar->setFixedHeight(50);
//    SET_ACC_NAME(titleBar, closeButton);
//    titleBar->setBackgroundTransparent(true);
//    titleBar->setMenuVisible(false);
//    titleBarLayout->addWidget(titleBar, 0, Qt::AlignRight | Qt::AlignTop);
//    titleBarLayout->setContentsMargins(0, 0, 0, 0);
//    mainlayout->addLayout(titleBarLayout);

    // 内容布局
    QVBoxLayout *contentLayout = new QVBoxLayout();
    contentLayout->setContentsMargins(10, 0, 10, 5);

    QLabel *titleLabel = new QLabel(tr("Whitelist"), this);
//    SET_ACC_NAME(titleLabel, titleLabel);
    //    titleLabel->setFixedHeight(20);
//    DFontSizeManager::instance()->bind(titleLabel, DFontSizeManager::T6);

    // 上部
    QHBoxLayout *topLayout = new QHBoxLayout();
    topLayout->setContentsMargins(0, 0, 0, 10);
    topLayout->setSpacing(0);

    // 添加顶部组件到横向布局
    topLayout->addWidget(titleLabel, 0, Qt::AlignCenter | Qt::AlignTop);
    contentLayout->addLayout(topLayout);

    // 中间表格布局
    QWidget *CenterWidget = new QWidget(this);
    SET_ACC_NAME(CenterWidget, centerWidget);
    CenterWidget->setMinimumWidth(TABLEVIEW_WIDTH);
    CenterWidget->setMinimumHeight(TABLEVIEW_HEIGHT);
    CenterWidget->setLayout(initTableWidget());
    contentLayout->addWidget(CenterWidget);

    // 底部
    QHBoxLayout *bottomLayout = new QHBoxLayout;
    QLabel *logTableTip = new QLabel(tr("Add devices to the whitelist from connection records"));
//    DFontSizeManager::instance()->bind(logTableTip, DFontSizeManager::T8);
//    logTableTip->setElideMode(Qt::ElideRight);
    SET_ACC_NAME(logTableTip, logTip);
    bottomLayout->addWidget(logTableTip);
    bottomLayout->addStretch(1);

    // 添加一个模板下载按钮
    m_downloadBtn = new QCommandLinkButton(tr("Get templates"));
//    DFontSizeManager::instance()->bind(m_downloadBtn, DFontSizeManager::T8);
    SET_ACC_NAME(m_downloadBtn, downloadButton);
    bottomLayout->addWidget(m_downloadBtn);

    // 添加一个导出按钮
    m_importBtn = new QCommandLinkButton(tr("Import whitelists"));
//    DFontSizeManager::instance()->bind(m_importBtn, DFontSizeManager::T8);
    SET_ACC_NAME(m_importBtn, importButton);
    bottomLayout->addWidget(m_importBtn);

    contentLayout->addLayout(bottomLayout);
    mainlayout->addLayout(contentLayout);
}

void USBWhiteListDlg::initConnection()
{
    // 有对象插入时创建下拉框
    connect(m_standardItemModel, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(createEditors(const QModelIndex &, int, int)));

    // 点击详情页图标显示详情页
    connect(m_USBWhitelistDelegate, SIGNAL(notifyShowUSBDetails(USBConnectionInfo &)), this, SLOT(showDetailsDlg(USBConnectionInfo &)));

    // 白名单  白名单移除
    connect(m_USBWhitelistDelegate, SIGNAL(notifyOperateWhitelist(int, const USBConnectionInfo &)),
            this, SLOT(onNotifyOperateWhitelist(int, const USBConnectionInfo &)));

    // 白名单  下拉框权限设置修改
    connect(m_USBWhitelistDelegate, SIGNAL(requestAuthorizeChangeDevPerm(const USBConnectionInfo &)),
            m_usbStorageSecurityModel, SIGNAL(requestAuthorizeChangeDevPerm(const USBConnectionInfo &)));

    // 但接收到白名单
    connect(m_usbStorageSecurityModel, SIGNAL(sendAllWhitelist(const USBConnectionInfoList &)), this, SLOT(onRecvAllWhitelist(const USBConnectionInfoList &)));
    // 白名单操作完成后 调整界面显示
    connect(m_usbStorageSecurityModel, SIGNAL(operateStorageWhitelistFinished(int, const USBConnectionInfo &)), this, SLOT(onOperateStorageWhitelistFinished(int, const USBConnectionInfo &)));

    // 批量导入白名单完成
    connect(m_usbStorageSecurityModel, SIGNAL(importBatchStorageWhitelistFinished(const USBConnectionInfoList &)), this, SLOT(onImportBatchStorageWhitelistFinished(const USBConnectionInfoList &)));

    // 白名单批量添加  DBUS批量添加完成后 界面添加
    connect(m_usbStorageSecurityModel, SIGNAL(addBatchUSBWhitelistBatchFinished(const USBConnectionInfoList &)), this, SLOT(addBatchWhitelist(const USBConnectionInfoList &)));

    // 白名单批量修改  DBUS批量修改完成后 界面修改
    connect(m_usbStorageSecurityModel, SIGNAL(modifyBatchUSBWhitelistBatchFinished(const USBConnectionInfoList &)), this, SLOT(modifyBatchWhitelist(const USBConnectionInfoList &)));

    // 白名单界面是否启用状态刷新
    connect(m_usbStorageSecurityModel, SIGNAL(notifyWhitelistDlgUpdate()), this, SLOT(updateUI()));

    // USB白名单模板下载
    connect(m_downloadBtn, SIGNAL(clicked(bool)), m_usbStorageSecurityModel, SLOT(exportWhitelistTemplate()));

    // USB白名单导入按钮响应
    connect(m_importBtn, SIGNAL(clicked()), m_usbStorageSecurityModel, SIGNAL(requestConfirmImportWhitelist()));

    // 新连接USB的Name改变，刷新白名单Name显示
    connect(m_usbStorageSecurityModel, SIGNAL(notifyUpdateNameAndSizeInWhitelist(const USBConnectionInfo &, const QString &, qint64 )), this, SLOT(updateNameAndSizeInWhitelist(const USBConnectionInfo &, const QString &, qint64)));
}

// 初始化tableview
QHBoxLayout *USBWhiteListDlg::initTableWidget()
{
    // 表格属性设置 文件 操作
    QHBoxLayout *layout = new QHBoxLayout();

    // DFrame 作用 -- 表格边缘线
    QFrame *tableFrame = new QFrame(this);
    layout->addWidget(tableFrame);
    QVBoxLayout *tableLyaout = new QVBoxLayout;
    tableFrame->setLayout(tableLyaout);
    tableLyaout->setContentsMargins(0, 0, 0, 6);

    layout->setMargin(0);
    layout->setSpacing(0);

    m_whitelistTable = new QTableView(this);
    SET_ACC_NAME(m_whitelistTable, tableView);

    m_whitelistTable->setSelectionBehavior(QAbstractItemView::SelectRows); // 整行选中的的方式
    m_whitelistTable->setSelectionMode(QAbstractItemView::NoSelection); // 设置选中单个目标
    m_whitelistTable->setEditTriggers(QAbstractItemView::NoEditTriggers); // 设置禁止编辑
    m_whitelistTable->setFrameShape(QTableView::NoFrame); // 设置无边框
    // 自动调整最后一列的宽度使它和表格的右边界对齐
    m_whitelistTable->horizontalHeader()->setStretchLastSection(true);
    m_whitelistTable->verticalHeader()->setHidden(true); // 列表头隐藏
    m_whitelistTable->setShowGrid(false);
    m_whitelistTable->verticalHeader()->setDefaultSectionSize(48); // 行高设置

    // 设置代理
    m_USBWhitelistDelegate = new USBWhitelistDelegate(this);
    m_whitelistTable->setItemDelegate(m_USBWhitelistDelegate);

    // 连接表格与模型
    m_standardItemModel = new QStandardItemModel(this);
    m_whitelistTable->setModel(m_standardItemModel);

    // 初始化表格设置
    // 设置表头
    // 初始化表头
    m_headerDelegate = new DefenderTableHeaderView(Qt::Horizontal, this);
    // 表头行高设置
    m_headerDelegate->setFixedHeight(36);
    // 设置更换表头
    m_whitelistTable->setHorizontalHeader(m_headerDelegate);

    QStandardItem *item_0 = new QStandardItem(tr("Device"));
    item_0->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    QStandardItem *item_1 = new QStandardItem(tr("Time Added"));
    item_1->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    QStandardItem *item_2 = new QStandardItem(tr("Policy"));
    item_2->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    QStandardItem *item_3 = new QStandardItem(tr("Action"));
    item_3->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    m_standardItemModel->setHorizontalHeaderItem(USB_NAME_COL, item_0);
    m_standardItemModel->setHorizontalHeaderItem(USB_TIME_COL, item_1);
    m_standardItemModel->setHorizontalHeaderItem(USB_STRATEGY_COL, item_2);
    m_standardItemModel->setHorizontalHeaderItem(USB_BTN_COL, item_3);

    // 设置Model数据
    m_whitelistTable->setModel(m_standardItemModel);

    // 屏蔽 自动排序&表头排序图标
    m_whitelistTable->setSortingEnabled(false);
    m_whitelistTable->horizontalHeader()->setSortIndicatorShown(false);

    tableLyaout->addWidget(m_whitelistTable);

    // 设置列宽
    m_whitelistTable->setColumnWidth(USB_NAME_COL, 190);
    m_whitelistTable->setColumnWidth(USB_TIME_COL, 190);
    m_whitelistTable->setColumnWidth(USB_STRATEGY_COL, 150);
    m_whitelistTable->setColumnWidth(USB_BTN_COL, 150);

    // 添加数据到 StandItem
    QList<USBConnectionInfo> usbConnectionInfoLst = m_usbStorageSecurityModel->usbConnectionWhiteLst();

    for (int i = 0; i < usbConnectionInfoLst.size(); i++) {
        USBConnectionInfo info = usbConnectionInfoLst.at(i);
        appendWhitelist(info);
    }

    return layout;
}

void USBWhiteListDlg::insertWhitelist(int row, const USBConnectionInfo &info)
{
    // 数据项
    QStandardItem *dataItem = createDataItemFromInfo(info);
    QList<QStandardItem *> itemLst;

    // 第1列 USB名称
    QString deviceName = dataItem->data(DATA_ROLE_NAME).toString();
    QStandardItem *nameItem = dataItem; // 使用名称列存储数据
    nameItem->setData(deviceName, Qt::ItemDataRole::EditRole);
    nameItem->setToolTip(deviceName);
    // 第2列 时间项
    QString timeAdd = dataItem->data(DATA_ROLE_TIME).toString();
    QStandardItem *timeItem = new QStandardItem(timeAdd);
    timeItem->setToolTip(timeAdd);

    // 第3列 限制状态项
    int strategy = dataItem->data(DATA_ROLE_STRATEGY).toInt();
    QStandardItem *strategyItem = new QStandardItem;
    //设置限制状态
    if (USB_MANAGER_PERM_MODE_RW == strategy) {
        strategyItem->setText(tr("Read-write"));
    } else if (USB_MANAGER_PERM_MODE_RO == strategy) {
        strategyItem->setText(tr("Read only"));
    } else {
        strategyItem->setText(tr("Block"));
    }
    // 此处不让显示，通过ComboBox 显示
    strategyItem->setText("");
    // 第4列 白名单操作项
    QStandardItem *btnItem = new QStandardItem;

    itemLst << nameItem << timeItem << strategyItem << btnItem;
    m_standardItemModel->insertRow(row, itemLst);
}

void USBWhiteListDlg::appendWhitelist(const USBConnectionInfo &info)
{
    insertWhitelist(0, info);

    // 默认排序
    m_whitelistTable->sortByColumn(USB_TIME_COL, Qt::SortOrder::DescendingOrder);
}

// 移除白名单
void USBWhiteListDlg::removeWhitelist(const USBConnectionInfo &info)
{
    QModelIndex findIndex;
    if (!getModelIndex(findIndex, info)) {
        return;
    }

    int findRow = findIndex.row();
    // 更新代理数据和控件
    m_standardItemModel->removeRow(findRow);
}

void USBWhiteListDlg::modifyWhitelist(const USBConnectionInfo &info)
{
    QModelIndex findIndex;
    if (!getModelIndex(findIndex, info)) {
        return;
    }

    int findRow = findIndex.row();
    // 更新代理数据和控件
    m_standardItemModel->removeRow(findRow);
    insertWhitelist(findRow, info);
}

QStandardItem *USBWhiteListDlg::createDataItemFromInfo(const USBConnectionInfo &info)
{
    // 数据项
    QStandardItem *dataItem = new QStandardItem;

    // 设备名
    QString deviceName;
    if (info.name.isEmpty()) {
        const QString &formatedName = QString("%1 ").arg(Utils::formatBytes(info.size, 1));
        deviceName = QString(tr("%1 Volume")).arg(formatedName);
    } else {
        deviceName = info.name;
    }
    dataItem->setData(deviceName, DATA_ROLE_NAME);

    // 添加时间
    dataItem->setData(info.addedTime, DATA_ROLE_TIME);
    // 连接状态
    dataItem->setData(info.permMode, DATA_ROLE_STRATEGY);
    // 序列号
    dataItem->setData(info.serial, DATA_ROLE_SERIAL);
    // 经销商id
    dataItem->setData(info.vendorID, DATA_ROLE_VENDOR_ID);
    // 制造商id
    dataItem->setData(info.productID, DATA_ROLE_PRODUCT_ID);
    // 添加/移除白名单状态
    dataItem->setData(USB_MANAGER_OP_TYPE_REMOVE, DATA_ROLE_OP_STATUS);

    return dataItem;
}

void USBWhiteListDlg::createEditor()
{
    int size = 0;
    int itemsCount = m_standardItemModel->rowCount();
    while (size < itemsCount) {
        QModelIndex tmp = m_standardItemModel->index(size, USB_STRATEGY_COL);
//        if (m_whitelistTable->isPersistentEditorOpen(tmp)) {
            m_whitelistTable->closePersistentEditor(tmp);
//        }
        m_whitelistTable->openPersistentEditor(tmp);
        size++;
    }
}

void USBWhiteListDlg::createEditors(const QModelIndex &parent, int first, int last)
{
    Q_UNUSED(parent);
    while (last >= first) {
        QModelIndex tmp = m_standardItemModel->index(first, USB_STRATEGY_COL);
//        if (m_whitelistTable->isPersistentEditorOpen(tmp)) {/
            m_whitelistTable->closePersistentEditor(tmp);
//        }
        m_whitelistTable->openPersistentEditor(tmp);
        first++;
    }
}

bool USBWhiteListDlg::getModelIndex(QModelIndex &index, const USBConnectionInfo &info)
{
    for (int i = m_standardItemModel->rowCount() - 1; i >= 0; i--) {
        const QModelIndex &indexTmp = m_standardItemModel->index(i, USB_DATA_COL);
        const QString serial = indexTmp.data(DATA_ROLE_SERIAL).toString();
        const QString vendorID = indexTmp.data(DATA_ROLE_VENDOR_ID).toString();
        const QString ProductID = indexTmp.data(DATA_ROLE_PRODUCT_ID).toString();
        // 通过USB 序列号匹配
        if (info.serial == serial && info.vendorID == vendorID && info.productID == ProductID) {
            index = indexTmp;
            return true;
        }
    }

    return false;
}

USBConnectionInfo USBWhiteListDlg::getInfoFromIndex(QModelIndex &index)
{
    USBConnectionInfo info;
    info.name = index.data(DATA_ROLE_NAME).toString();
    info.addedTime = index.data(DATA_ROLE_TIME).toString();
    info.permMode = index.data(DATA_ROLE_STRATEGY).toInt();
    info.serial = index.data(DATA_ROLE_SERIAL).toString();
    info.vendorID = index.data(DATA_ROLE_VENDOR_ID).toString();
    info.productID = index.data(DATA_ROLE_PRODUCT_ID).toString();

    return info;
}

void USBWhiteListDlg::updateUI()
{
    // 刷新状态
    setEnabled(m_usbStorageSecurityModel->getUIEnableStatus());
}

// 批量添加白名单 界面改变
void USBWhiteListDlg::addBatchWhitelist(const USBConnectionInfoList &list)
{
    for (int i = 0; i < list.size(); i++) {
        USBConnectionInfo info = list.at(i);
        appendWhitelist(info);
    }
}

// 批量修改白名单 界面改变
void USBWhiteListDlg::modifyBatchWhitelist(const USBConnectionInfoList &list)
{
    for (int i = 0; i < list.size(); i++) {
        USBConnectionInfo info = list.at(i);
        modifyWhitelist(info);
    }
}

// 新连接USB的Name改变，刷新白名单Name显示
void USBWhiteListDlg::updateNameAndSizeInWhitelist(const USBConnectionInfo &info, const QString &name, qint64 size)
{
    QModelIndex findIndex;
    if (!getModelIndex(findIndex, info)) {
        return;
    }

    int findRow = findIndex.row();
    // 更改名称和容量
    USBConnectionInfo findedInfo = getInfoFromIndex(findIndex);
    findedInfo.name = name;
    findedInfo.size = size;

    // 更新代理数据和控件
    m_standardItemModel->removeRow(findRow);
    insertWhitelist(findRow, findedInfo);
}

static int gType;
static USBConnectionInfo gInfo;
void USBWhiteListDlg::onNotifyOperateWhitelistHelp(int index, const QString &)
{
    switch (index) {
    case 0:
        break;
    case 1:
        // 对USB白名单 进行设置  增删改
        m_usbStorageSecurityModel->asyncOperateStorageWhitelist(gType, gInfo);
        break;
    default:
        break;
    }
}

void USBWhiteListDlg::showDetailsDlg(USBConnectionInfo &info)
{
    // USB设备详情页弹窗
    m_USBDeviceDetails = new USBDeviceDetailsDlg(m_usbStorageSecurityModel, info, this);
    m_USBDeviceDetails->raise();
    m_USBDeviceDetails->show();
}

void USBWhiteListDlg::onRecvAllWhitelist(const USBConnectionInfoList &list)
{
    // 若已经接收过初始连接记录，则不重复添加到表格
    if (0 < m_standardItemModel->rowCount()) {
        return;
    }

    for (int i = 0; i < list.size(); i++) {
        const USBConnectionInfo &info = list.at(i);
        appendWhitelist(info);
    }
}

// delegate 响应白名单操作
void USBWhiteListDlg::onNotifyOperateWhitelist(int type, const USBConnectionInfo &info)
{
    // 白名单移除：弹出二次确认弹窗
    if (type == USB_MANAGER_OP_TYPE_REMOVE) {
        // 用户确认
        DDialog *dialog = new DDialog(this);
        SET_ACC_NAME(dialog, RemoveWhitelistConfirmDialog);
        dialog->setModal(true);
        dialog->setOnButtonClickedClose(true);
        // 设置关闭后删除
        dialog->setAttribute(Qt::WA_DeleteOnClose);
        // 设置左上角图标
        dialog->setIcon(QIcon::fromTheme(DIALOG_TIP_YELLOW));
        dialog->setMessage(tr("Are you sure you want to remove it from the whitelist?"));
        dialog->setWordWrapMessage(true);

        // 针对DTK弹窗目前accessibleName会随系统语言变化的情况
        // 通过insertButton的形式定制按钮,保持accessiableName的一致性
        QPushButton *cancelBtn = new QPushButton(dialog);
        cancelBtn->setText(tr("Cancel", "button"));
        cancelBtn->setObjectName("cancelBtn");
        SET_ACC_NAME(cancelBtn, RemoveWhitelistConfirmDialog_cancelButton);
        cancelBtn->setAttribute(Qt::WA_NoMousePropagation);
        dialog->insertButton(0, cancelBtn, false);

        QPushButton *exitBtn = new QPushButton(dialog);
        exitBtn->setText(tr("Confirm", "button"));
        exitBtn->setObjectName("confirmBtn");
        SET_ACC_NAME(exitBtn, RemoveWhitelistConfirmDialog_confirmButton);
        exitBtn->setAttribute(Qt::WA_NoMousePropagation);
        dialog->insertButton(1, exitBtn, true);

        QLabel *messageLable = dialog->findChild<QLabel *>("MessageLabel");
        SET_ACC_NAME(messageLable, RemoveWhitelistConfirmDialog_messageLable);

        gType = type;
        gInfo = info;
        connect(dialog, SIGNAL(buttonClicked(int index, const QString &)), this, SLOT(onNotifyOperateWhitelistHelp(int, const QString &)));
        dialog->show();
    } else {
        m_usbStorageSecurityModel->asyncOperateStorageWhitelist(type, info);
    }
}

void USBWhiteListDlg::onOperateStorageWhitelistFinished(int opType, const USBConnectionInfo &info)
{
    switch (opType) {
    case USB_MANAGER_OP_TYPE_ADD:
        addBatchWhitelist({info});
        break;
    case USB_MANAGER_OP_TYPE_REMOVE:
        removeWhitelist(info);
        break;
    case USB_MANAGER_OP_TYPE_MODIFY:
        modifyWhitelist(info);
        break;
    default:
        break;
    }
}

void USBWhiteListDlg::onImportBatchStorageWhitelistFinished(const USBConnectionInfoList &list)
{
    Q_UNUSED(list)
    // 显示应用内弹窗显示导入成功
    m_usbStorageSecurityModel->showFloatMsg(this, DIALOG_OK_TIP_GREEN, tr("Import successful"));
}

void USBWhiteListDlg::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    deleteLater();
}
